// 放大镜
$('.smallBox').mouseover(function(e){
	$('.bigBox').css('display','block');
	$('.mask').css('display','block');
})

$('.smallBox').mousemove(function(e){
	var pageX=e.pageX;
	var pageY=e.pageY;

	var offsetX=$('.smallBox').offset().left;
	var offsetY=$('.smallBox').offset().top;

	var relativeX=pageX-offsetX;
	var relativeY=pageY-offsetY;

	var magOffsetX=$('.mask').width()/2;
	var magOffsetY=$('.mask').height()/2;

	$('.mask').css({
        left:relativeX-magOffsetX+'px',
        top:relativeY-magOffsetY+'px'
    });

	var magX=$('.mask').position().left;
	var magY=$('.mask').position().top;

	var maxMagX=$('.smallBox').width()-$('.mask').width()
	var maxMagY=$('.smallBox').height()-$('.mask').height()

	if(magX<=0){$('.mask').css('left','0px');}

	if(magX>=maxMagX){$('.mask').css('left',maxMagX+'px');}

	if(magY<=0){$('.mask').css('top','0px');}

	if(magY>=maxMagY){$('.mask').css('top',maxMagY+'px');}
	
	var originX=magX*3;
	var originY=magY*3;
	$('.bigBox').css({
            backgroundPositionX:-originX+'px',
            backgroundPositionY:-originY+'px'
        })
	});

$('.smallBox').mouseout(function(){
	$('.bigBox').css('display','none');
	$('.mask').css('display','none');
})

// 底部的li
// 请求商品详情数据
$(function() {
	var data = window.location.href.split("?")[1].split("=")[1];

	let xhr = new XMLHttpRequest();
	xhr.open("get", "goodsAndShoppingCart/getGoodsInfo.php?goodsId=" + data, true);
	xhr.send();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			fun(xhr.responseText);
		}
	}
	function fun(restext) {
		let newArr = JSON.parse(restext);

		$(".eleType-title span").html(newArr.goodsType);
		$(".eleType-title b").html(newArr.goodsDesc);
		$(".rightTit h3").html(newArr.goodsName);
		$(".rightTit p").html(newArr.goodsDesc);
		$(".rightPri b").html(newArr.goodsPrice);

		console.log(newArr.goodsId);

		$(".carBtn").attr("id",`${newArr.goodsId}`);

		$(".li_url1").css({
			background:`url(${newArr.beiyong1}) no-repeat center center`,
			backgroundSize:"60px 60px"
		})
		$(".li_url2").css({
			background:`url(${newArr.beiyong2}) no-repeat center center`,
			backgroundSize:"60px 60px"
		})
		$(".li_url3").css({
			background:`url(${newArr.beiyong3}) no-repeat center center`,
			backgroundSize:"60px 60px"
		})
		$(".li_url4").css({
			background:`url(${newArr.beiyong4}) no-repeat center center`,
			backgroundSize:"60px 60px"
		})
		$(".li_url5").css({
			background:`url(${newArr.beiyong5}) no-repeat center center`,
			backgroundSize:"60px 60px"
		})

		$(".smallBox").css({
			backgroundImage: `url(${newArr.goodsImg})`,
		})
		$(".bigBox").css({
			backgroundImage: `url(${newArr.goodsImg})`,
		})
		
		let olis=document.querySelectorAll(".tab_con_li");
		for (let i = 0; i < olis.length; i++) {
			olis[i].onmouseover = function() {
				switch(i){
					case 0:
						$(".smallBox").css({"background-image": `url(${newArr.beiyong1})`,})
						$(".bigBox").css({"background-image": `url(${newArr.beiyong1})`,})
					break;
					case 1:
						$(".smallBox").css({"background-image": `url(${newArr.beiyong2})`,})
						$(".bigBox").css({"background-image": `url(${newArr.beiyong2})`,})
					break;
					case 2:
						$(".smallBox").css({"background-image": `url(${newArr.beiyong3})`,})
						$(".bigBox").css({"background-image": `url(${newArr.beiyong3})`,})
					break;
					case 3:
						$(".smallBox").css({"background-image": `url(${newArr.beiyong4})`,})
						$(".bigBox").css({"background-image": `url(${newArr.beiyong4})`,})
					break;
					case 4:
						$(".smallBox").css({"background-image": `url(${newArr.beiyong5})`,})
						$(".bigBox").css({"background-image": `url(${newArr.beiyong5})`,})
					break;
				}
			}
		}

	}
})

// 添加到购物车
$(".carBtn").click(function(){
	
	$.post(
		"goodsAndShoppingCart/addShoppingCart.php",{
			"vipName":getCookie("username"),
			"goodsId":this.id
	},
	function(data){
		if (data == "1") {
			console.log(111);
		} else if (data == "0") {
			console.log(000);
		} else {
			console.log(333);
			window.location.href = "shoppingCar.html"
		}
	}
	)
})